<?php
namespace common\modules\storage\backend\controllers;

use common\modules\storage\models\FileStorageItem;
use common\modules\storage\backend\models\search\FileStorageItemSearch;
use common\modules\storage\actions\DeleteAction;
use common\modules\storage\actions\UploadAction;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * FileStorageController implements the CRUD actions for FileStorageItem model.
 */
class FileStorageController extends Controller
{

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => [
                        'post'
                    ],
                    'upload-delete' => [
                        'delete'
                    ]
                ]
            ]
        ];
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => UploadAction::className(),
                'deleteRoute' => 'upload-delete',
                'pathFormat' => '{yyyy}{mm}{dd}/{rand:8}'
            ],
            'upload-delete' => [
                'class' => DeleteAction::className()
            ]
        ];
    }

    /**
     * Lists all FileStorageItem models.
     * 
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new FileStorageItemSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->sort = [
            'defaultOrder' => [
                'created_at' => SORT_DESC
            ]
        ];
        $components = \yii\helpers\ArrayHelper::map(FileStorageItem::find()->select('component')
            ->distinct()
            ->all(), 'component', 'component');
        $totalSize = FileStorageItem::find()->sum('size') ?  : 0;
        
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'components' => $components,
            'totalSize' => $totalSize
        ]);
    }

    /**
     * Displays a single FileStorageItem model.
     * 
     * @param integer $id            
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id)
        ]);
    }

    /**
     * Deletes an existing FileStorageItem model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * 
     * @param integer $id            
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        
        return $this->redirect([
            'index'
        ]);
    }

    /**
     * Finds the FileStorageItem model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * 
     * @param integer $id            
     * @return FileStorageItem the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = FileStorageItem::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
